package com.example.certificate.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.certificate.dto.ProductDTO;
import com.example.certificate.dto.ProductDescDTO;
import com.example.certificate.dto.SearchDTO;
import com.example.certificate.dto.SelProductDTO;
import com.example.certificate.entity.ProductInfo;
import com.example.certificate.entity.User;
import com.example.certificate.execption.BusinessException;
import com.example.certificate.mapper.ProductInfoMapper;
import com.example.certificate.service.ProductInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hao
 * @since 2022-03-10 12:42:47
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements ProductInfoService {


    @Autowired
    private ProductInfoMapper productInfoMapper;


    /**
     * 添加商品
     *
     * @param dto
     * @param user
     */
    @Override
    @Transactional(transactionManager = "transactionManager",propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void addProduct(ProductDTO dto, User user) throws BusinessException {
        ProductInfo productInfo=new ProductInfo();
        productInfo.setBusinessId(dto.getBusinessId()); //业务分类
        productInfo.setUserId(user.getId()); //用户名
        productInfo.setTradeName(dto.getTradeName()); //名称
        productInfo.setTradeDescribe(dto.getTradeDescribe()); //描述
        productInfo.setTradePrice(dto.getTradePrice()); //价格
        productInfo.setOriginalPrice(dto.getOriginalPrice()); //原价
        productInfo.setEstimatedTime(dto.getEstimatedTime()); //预计到达时间
        productInfoMapper.insert(productInfo); //添加
    }

    /**
     * 分页查询业务分类
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional(value = "transactionManager",propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Page<ProductInfo> selProduct(SelProductDTO dto) {
        Page<ProductInfo> page=new Page<ProductInfo>(); //分页
        page.setCurrent(dto.getCurrent()==null?1:dto.getCurrent()); //当前页数
        page.setSize(dto.getSize()==null?10:dto.getSize()); //每页大小
        page = productInfoMapper.selectPage(page,new QueryWrapper<ProductInfo>().eq("businessId",dto.getBusiness())); //查询业务数据
        page.getRecords().forEach(item->{
            });
        return page;
    }

    /**
     * 查询商品详情
     *
     * @param descDTO
     * @return
     */
    @Override
    public ProductInfo proDetail(ProductDescDTO descDTO) {
        //查询商品信息
        ProductInfo productInfo = productInfoMapper.selectOne(new QueryWrapper<ProductInfo>().eq("id",descDTO.getProductId()));
        return productInfo; //返回前端
    }

    /**
     * 模糊查询商品
     *
     * @param dto
     * @return
     */
    @Override
    public Page<ProductInfo> searchProduct(SearchDTO dto) {
        Page<ProductInfo> page = new Page<ProductInfo>();
        page.setCurrent(dto.getCurrent() == null ? 1 : dto.getCurrent());
        page.setSize(dto.getSize() == null ? 10 : dto.getSize());
        return page.setRecords(null);
    }

    /**
     * 查询近日优品数据
     *
     * @param page
     */
    @Override
    public Page<ProductInfo> todayProduct(Page<ProductInfo> page) {
        page = productInfoMapper.selectPage(page,new QueryWrapper<ProductInfo>().in("businessId","a7aad1ed585d480eb9dc016d3485ef1b"));
        if(page.getRecords()!=null && !CollectionUtils.isEmpty(page.getRecords())){
            page.getRecords().forEach(item->{

            });
        }
        return page;
    }
}
